package factory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import DAO.ProductDAO;
import to.MaterialTO;
import to.OrderTO;
import to.ProductTO;
import util.DBUtil;
import util.MaterialParser;
import util.ProductParser;

public class Generator implements Observed, Runnable{
	private static final double min = 40000;
	private static final double max = 80000;
	private ObserverManager observer;
	private int currentProductId;
	private HashMap<String, String> typeMap = new HashMap();
	private HashMap<String, String> colorMap = new HashMap();
	
	public Generator() throws SQLException {
		ProductDAO dao = ProductDAO.getInstance();
		observer = ObserverManager.getInstance();
		typeMap = dao.selectProductType();
		colorMap = dao.selectMaterial();
		currentProductId = dao.selectCurrentProductionId()+1;
		new Thread(this).start();
	}
	
	private void generate(){
		OrderTO order = new OrderTO();
		
		//OrderTO 셋팅
		String typeId = "P"+((int)(Math.random()*6)+1); //1~6
		int amount = (int)(Math.random()*30)+50;
		int size = (int)(Math.random()*4)*5+90; //90,95,100,105,110
		String colorId = "M"+((int)(Math.random()*6)+3);
		int countButtons = 0;
		if(typeId.equals("P2")){
			countButtons = (int)(Math.random() * 5);
		}
		if(typeId.equals("P3")){
			countButtons = (int)(Math.random() * 6) + 5;
		}
		
		order.setOrderAmount(amount);
		order.setTypeId(typeId);
		order.setTypeName(typeMap.get(typeId));
		order.setColorId(colorId);
		order.setColorName(colorMap.get(colorId));
		order.setSize(size);
		order.setCountButtons(countButtons);
		
		ProductParser pp = ProductParser.getInstance();
		for(int i=0; i<=order.getOrderAmount()-1; i++){
			ProductTO product = new ProductTO();
			product.setProductId(currentProductId++);
			product.setOrderId(order.getOrderId());
			order.getProductList().add(product);
			pp.writeProduct("cutting", product);
		}
		
		//MaterialTO 셋팅
		ArrayList<MaterialTO> materialList = new ArrayList();
		MaterialParser mp = MaterialParser.getInstance();
		MaterialTO cotton = mp.readMaterial("M1");
		MaterialTO thread = mp.readMaterial("M2");
		MaterialTO button = mp.readMaterial("M3");
		MaterialTO dye = mp.readMaterial(colorId);

		cotton.setQuantity(cotton.getQuantity()+(int)(order.getSize()*order.getOrderAmount()));
		thread.setQuantity(thread.getQuantity()+(int)(order.getSize()*order.getOrderAmount()));
		button.setQuantity(button.getQuantity()+(int)(order.getCountButtons()*order.getOrderAmount()));
		dye.setQuantity(dye.getQuantity()+(int)(order.getSize()*order.getOrderAmount()));
		
		mp.writeMaterial(cotton);
		mp.writeMaterial(thread);
		mp.writeMaterial(button);
		mp.writeMaterial(dye);
		mp.returnKey();
		
		update(order);
	}

	public void update(OrderTO order) {
		observer.notifyOrder(order);
	}

	@Override
	public void run() {
		while(true){
			//40초~80초에 한번 주문 생성
			int sleepingDuration = (int)(Math.random() * (max-min) + min); 	
		
			generate();
			
			try {
				Thread.sleep(sleepingDuration);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
